package com.fzy.core.system.action;

import ge.core.system.entity.Entity;

import com.fzy.core.system.service.DbService;
import com.fzy.core.webwork.BaseAction;
import com.fzy.core.webwork.MyValidationAware;
import com.opensymphony.xwork.ModelDriven;
import com.opensymphony.xwork.Preparable;

public class EntitySaveAction extends BaseAction implements Preparable, MyValidationAware, ModelDriven {
	
	private static final long serialVersionUID = 2173896425512334073L;
	String errorDispatch;
	Entity entity;
	DbService dbService;

	public void prepare() throws Exception {
		errorDispatch = "dispatcher:/pages/refresh-parent.jsp";
		entity = (Entity)validateIdAndGetEntity(request.getParameter("id"), Entity.class);
        
		// default values
        if(entity == null){
        	entity = new Entity();
        	entity.setPackageText("ge.app");
        	entity.setHasPrefix("1");
    		entity.setPrefixText("ru");
    		entity.setIsWork("1");
    		entity.setIsParentMapping("1");
        }
	}

	public void validate() {

	}

	public String execute() throws Exception {
		dbService.saveEntityId(entity);

		addActionMessage(getText("success.save"));
		return "dispatcher:/pages/refresh-parent.jsp";
	}

	public Object getModel() {
		return entity;
	}

	public String getErrorDispatch() {
		return errorDispatch;
	}

	public void setDbService(DbService dbService) {
		this.dbService = dbService;
	}

}
